package com.vonchange.utils.tpl.help;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.vonchange.utils.file.FileUtils;
import com.vonchange.utils.res.compressor.TResCompressorUtils;
import com.vonchange.utils.tpl.bean.ResCacheBean;
import com.vonchange.utils.web.RequestUtils;

/**
 * 
 *
 * @author von_change@163.com
 * @date 2015年12月4日 下午12:56:22
 * @since 1.0
 */
public  class ResConfigInitService {
	private static Log log = LogFactory.getLog(ResConfigInitService.class);
    private static  Boolean flag=false;
	private static File getFile(HttpServletRequest request,String path) {
		return new File(RequestUtils.getWebRealPath(request,path));
	}

	static void initJsConfig(HttpServletRequest request) {
		Map<String, String> configMap = ReadJsonToMap.read(getFile(request,"commonConfig/js.json"));
		for (Entry<String, String> entry : configMap.entrySet()) {
			String jsStr = entry.getValue();
			String[] jsSubSrcs = jsStr.split(",");
			for (String jsSubSrc : jsSubSrcs) {
				if(jsSubSrc.startsWith("@")){
					continue;
				}
				String jsPath = RequestUtils.getWebRealPath(request,jsSubSrc);
				File jsFile = new File(jsPath);
				if (!jsFile.exists()) {
					continue;
				}
				Long time = jsFile.lastModified();
				String jsContent ="";
				if(jsSubSrc.endsWith(".min.js")){
					try {
						jsContent= FileUtils.readFileToString(jsFile);
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}else{
					 jsContent = TResCompressorUtils.jsCompressorByFile(jsFile);
				}		
				ResCache.resCacheMap.put(jsSubSrc, new ResCacheBean().setContent(jsContent).setTime(time));
			}
		}
	}

	static void  initCssConfig(HttpServletRequest request) {
		Map<String, String> configMap = ReadJsonToMap.read(getFile(request,"commonConfig/css.json"));
		for (Entry<String, String> entry : configMap.entrySet()) {
			String cssStr = entry.getValue();
			String[] cssSubSrcs =cssStr.split(",");
			for (String cssSubSrc : cssSubSrcs) {
				if(cssSubSrc.startsWith("@")){
					continue;
				}
				String cssPath = RequestUtils.getWebRealPath(request,cssSubSrc);
				File cssFile = new File(cssPath);
				if (!cssFile.exists()) {
					continue;
				}
				Long time = cssFile.lastModified();
				String cssContent = TResCompressorUtils.cssCompressor(cssFile);
				ResCache.resCacheMap.put(cssSubSrc, new ResCacheBean().setContent(cssContent).setTime(time));
			}
		}
	}
	public static void init(HttpServletRequest request) {
		if(!flag){
			log.info("加载js和css缓存");
			try {
				initJsConfig(request);
				initCssConfig(request);
			} catch (Exception e) {
				e.printStackTrace();
				// TODO: handle exception
			}finally{
				flag=true;
			}
			flag=true;
		}
					
	}

}
